fix: include root cause in MCPClientInitializationError message#2238
Open
aidandaly24 wants to merge 1 commit intostrands-agents:mainfrom
Open
fix: include root cause in MCPClientInitializationError message#2238aidandaly24 wants to merge 1 commit intostrands-agents:mainfrom
aidandaly24 wants to merge 1 commit intostrands-agents:mainfrom
Conversation
When MCPClient.start() fails due to a transport or auth error, the exception message was a generic "the client initialization failed" that hid the actual root cause. The original exception was preserved in the __cause__ chain but not in the string representation, making it difficult to diagnose failures in managed environments where only the error message is visible (e.g., streaming event responses). Include str(e) in the error message so the root cause is visible without needing a full stack trace. Fixes strands-agents#2237
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When
MCPClient.start()fails due to a transport or auth error (e.g., OAuth token exchange failure, connection refused), theMCPClientInitializationErrorthrown contains a generic message"the client initialization failed"that hides the root cause. The original exception is preserved in__cause__(viafrom e) but not instr().In managed environments like AgentCore Harness, the error message is surfaced through a streaming event and the full stack trace is not available to the user. This makes it impossible to diagnose the root cause.
This PR includes
str(e)in the error message so the root cause is visible:Before:
MCPClientInitializationError: the client initialization failedAfter:
MCPClientInitializationError: the client initialization failed: Failed to get OAuth token for Gateway (provider=my-provider): Token has expiredThe change is at
src/strands/tools/mcp/mcp_client.pyline 223.Related Issues
Fixes #2237
Documentation PR
N/A — no documentation changes needed.
Type of Change
Bug fix
Testing
How have you tested the change?
Ran full MCP test suite: 142 passed, 0 failed
Updated
test_enter_with_initialization_exceptionto verify the root cause message is included in the errorVerified no warnings introduced in the MCP module
I ran
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.